//
//  CZVistorView.swift
//  WHWeiBo
//
//  Created by Li Changjian on 16/7/9.
//  Copyright © 2016年 Li Changjian. All rights reserved.
//

import UIKit
import SnapKit

class CZVistorView: UIView {
    
    var loginClosure: (() -> ())?
    
    
    
/// 中间可旋转的图片
    lazy var centerSmallIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
/// 中间图片的遮罩,懒加载
    lazy var coverImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
/// 中间的通用图片
    lazy var centerImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
/// 提示信息
    lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        return label
    
    }()
/// 注册按钮
    lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        return button
    }()
/// 登录按钮
    lazy var loginButton: UIButton = {
        
        let button = UIButton()

        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    // MARK: - 注册
    func registerAction() {
        loginClosure?()
    }
    // MARK: - 登录
    func loginAction() {
        loginClosure?()
    }
    // MARK: - 刷新view显示信息
    func updateInfos(message: String?, imageName: String?) {
        
        if let msg = message, imgName = imageName {
            
            messageLabel.text = msg
            centerImageView.image = UIImage(named: imgName)
            centerSmallIconView.hidden = true
            
        }else {
            centerSmallIconView.hidden = false
            startTheAnimation()
        
        }
    }
    // MARK: - 开启动画
    func startTheAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        centerSmallIconView.layer.addAnimation(animation, forKey: nil)
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.whiteColor()
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(centerSmallIconView)
        addSubview(coverImageView)
        addSubview(centerImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        centerSmallIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(centerSmallIconView)
        }
        centerImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(centerSmallIconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(centerSmallIconView.snp_bottom).offset(30)
            make.width.equalTo(244)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        
    }

}
